import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class TestReflect {

    // 通过反射拿到枚举对象
    public static void reflectPrivateConstructor(){
        try {
            Class<?> testEnum = Class.forName("TestEnum");
            Constructor<?> constructor = testEnum.getDeclaredConstructor(String.class,int.class,String.class,int.class);
            constructor.setAccessible(true);
            //      通过反射拿不到枚举对象             newInstance这里报错
            //if ((clazz.getModifiers() & Modifier.ENUM) != 0)
            //   throw new IllegalArgumentException("Cannot reflectively create enum objects");
            TestEnum color = (TestEnum) constructor.newInstance("绿色", 666,"bit",99);
            System.out.println(color);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        reflectPrivateConstructor();
    }
}
